<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close :close-on-press-escape="mode != 'show' ? false : true"
             @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
            <el-form-item label="客户" prop="custom_id">
				<el-input v-model="custom_name" placeholder="请选择客户" readonly @click="checkCustom"></el-input>
			</el-form-item>
			<el-form-item label="仓库" prop="ware_id">
                <el-select v-model="form.ware_id" filterable style="width: 100%" placeholder="请选择仓库" disabled>
                    <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="库位" prop="location_id">
                <el-select v-model="form.location_id" filterable style="width: 100%" placeholder="请选择库位" disabled>
                    <el-option v-for="item in locationList" :key="item.id" :label="item.position_name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="可出库数量" prop="number">
				<el-input v-model="number" placeholder="可出库数量" readonly></el-input>
			</el-form-item>
            <el-form-item label="出库数量" prop="num">
				<el-input v-model="form.num" placeholder="请输入出库数量" clearable @input="input"></el-input>
			</el-form-item>
            <el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
    <client-list v-if="dialog.save" ref="saveDialog" @success="customBack" @closed="dialog.save=false"></client-list>
</template>

<script>
import clientList from '@/views/sale/client/list/clientList.vue';
	export default {
  components: { clientList },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '出库',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					custom_id:"",
					ware_id: "",
					remark: "",
					location_id: "",
					num: "",
					goods_id: "",
				},
				//验证规则
				rules: {
					// ware_name: [{required: true, message: '请输入仓库名称'}]
				},
                dialog:{
                    save:false
                },
                number: "",
                custom_name: "",
                wareList: [],
                locationList: [],
			}
		},
		mounted() {
        this.getWare() // 仓库
		},
		methods: {
            checkCustom(){
                this.dialog.save = true
                this.$nextTick(() => {
                    this.$refs.saveDialog.open('add')
                })
            },
            customBack(data){
                this.form.custom_id = data.id
                this.custom_name = data.customer_name
            },
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.stock.out.add.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.goods_id = data.goods&&data.goods.id
                this.number = data.curr_stock
                this.form.ware_id = data.ware_id
                this.getLocation(data.ware_id);
                this.form.location_id = data.location_id
			},
            // 获取仓库
            async getWare(){
                var res = await this.$API.stock.ware.all.get();
                this.wareList = res.data
            },
            async getLocation(e){
                const res = await this.$API.stock.ware.location.post({ware_id:e})
                this.locationList = res.data
            },
            input(e){
                if(e > this.number){
                    this.$alert("出库数量不能大于库存数量", "提示", {type: 'error'})
                    this.form.num = this.number
                }
            },
		}
	}
</script>

<style>
</style>
